package com.halloon.android.ui.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.halloon.android.R;
import com.halloon.android.adapter.PrivateMessageListAdapter;
import com.halloon.android.bean.PrivateDataBean;
import com.halloon.android.data.ContentManager;
import com.halloon.android.data.DBManager;
import com.halloon.android.data.SettingsManager;
import com.halloon.android.util.Constants;
import com.halloon.android.view.PullToRefreshListView;
import com.halloon.android.view.PullToRefreshListView.OnRefreshListener;

public class PrivateMessageFragment extends SherlockFragment {
	
	private View loadingView;
	private PullToRefreshListView list;
	private PrivateMessageListAdapter adapter;
	private ArrayList<PrivateDataBean> privateDataBeans;
	
	private Context context;
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
		this.context = activity;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View root = inflater.inflate(R.layout.private_message_fragment, null, false);
	    
		loadingView = root.findViewById(R.id.loading);
		list = (PullToRefreshListView) root.findViewById(R.id.private_message_list);
		privateDataBeans = new ArrayList<PrivateDataBean>();
		Log.d(Constants.LOG_TAG, privateDataBeans.size() + "");
		adapter = new PrivateMessageListAdapter(context, privateDataBeans);
		list.setAdapter(adapter);
		loadData();
		
		root.setId(0);
		
		return root;
	}
	
	public void loadData(){
		AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>(){
			@Override
			protected void onPreExecute(){
				loadingView.setVisibility(View.VISIBLE);
				list.setVisibility(View.GONE);
			}

			@Override
			protected String doInBackground(Void... params) {
				ArrayList<PrivateDataBean> tmp_list;
				
				if(SettingsManager.getInstance(context).getPrivateMessageStatus() == DBManager.PRIVATE_MESSAGE_STATUS_INIT){
					tmp_list = ContentManager.getInstance(context).getPrivateHomeTimeLine("0", "0", "20", "0");
					if(tmp_list.size() != 0){
						HashMap<String, PrivateDataBean> privateLists = new HashMap<String, PrivateDataBean>();
						for(int i = 0; i < tmp_list.size(); i++){
							privateLists.put(tmp_list.get(i).getPubTime(), tmp_list.get(i));
						}
						
						DBManager.getInstance(context).addPrivateMessage(privateLists, true);
					}
				}else{
					tmp_list = DBManager.getInstance(context).getPrivateMessage();
				}
				
				privateDataBeans.clear();
				privateDataBeans.addAll(tmp_list);
				return null;
			}
			
			@Override
			protected void onPostExecute(String result){
				loadingView.setVisibility(View.GONE);
				list.setVisibility(View.VISIBLE);
				adapter.notifyDataSetChanged();
				super.onPostExecute(result);
			}
			
		};
		
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}else{
			task.execute();
		}
	}
	
	private void refreshData(){
		AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>(){
			@Override
			protected void onPreExecute(){
			}
			
			@Override
			protected String doInBackground(Void... params){
				ArrayList<PrivateDataBean> tmp_list = ContentManager.getInstance(context).getPrivateHomeTimeLine("0", "0", "20", "0");
				
				if(tmp_list.size() != 0){
					HashMap<String, PrivateDataBean> privateBeans = new HashMap<String, PrivateDataBean>();
					for(int i = 0; i < tmp_list.size(); i++){
						privateBeans.put(tmp_list.get(i).getPubTime(), tmp_list.get(i));
					}
					
					DBManager.getInstance(context).addPrivateMessage(privateBeans, true);
					
					privateDataBeans.clear();
					privateDataBeans.addAll(tmp_list);
				}
				
				
				
				return null;
			}
			
			@Override
			protected void onPostExecute(String result){
				adapter.notifyDataSetChanged();
				list.onRefreshComplete();
			}
		};
		
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}else{
			task.execute();
		}
	}
	
	@Override
	public void onResume(){
		super.onResume();
		loadingView.setVisibility(View.GONE);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		list.setOnRefreshListener(new OnRefreshListener(){
			@Override
			public void onRefresh(){
				refreshData();
			}
		});
	}
}
